<?php
	include('header.php');
?>	
<script>
	$(function() {
		//$( "#datepicker" ).datepicker( "option", "dateFormat", $( this ).val() );
		$( "#dateFrom" ).datepicker({
			defaultDate: "-2w",
			dateFormat: 'dd/mm/yy',
			changeMonth: true,
			numberOfMonths: 2,
			onSelect: function( selectedDate ) {
				$( "#dateTo" ).datepicker( "option", "minDate", selectedDate );
			}
		});
		$( "#dateTo" ).datepicker({
			defaultDate: "-2w",
			dateFormat: 'dd/mm/yy',
			changeMonth: true,
			numberOfMonths: 2,
			onSelect: function( selectedDate ) {
				$( "#dateFrom" ).datepicker( "option", "maxDate", selectedDate );
			}
		});	
	});

</script>
		<!--[if !IE]>start content<![endif]-->					
		<div id="content">
			<div class="inner">
				
				<!--[if !IE]>start section<![endif]-->
				<?php

				

				?>
		
				<div class="section">
					
					<!--[if !IE]>start title wrapper<![endif]-->
					<div class="title_wrapper">
						<span class="title_wrapper_top"></span>
						<div class="title_wrapper_inner">
							<span class="title_wrapper_middle"></span>
							<div class="title_wrapper_content"> 
								<h2>Report Package</h2>
							</div>
						</div>
						<span class="title_wrapper_bottom"></span>
					</div>
					<!--[if !IE]>end title wrapper<![endif]-->
					
					<!--[if !IE]>start section content<![endif]-->
					<div class="section_content">
						<span class="section_content_top"></span>
						
						<div class="section_content_inner">
							<?php
								 
								$dateFrom = mysql_real_escape_string($_GET['dateFrom']); 
								$dateTo = mysql_real_escape_string($_GET['dateTo']); 
								$statuscode = mysql_real_escape_string($_GET['statuscode']); 
								$transportid = mysql_real_escape_string($_GET['transportid']); 
								$tdnNo = mysql_real_escape_string($_GET['tdnNo']); 
								
								if(isset($_POST['reset'])){
									$_POST = "";
								}

								if(isset($_POST['search'])){ 
									
									if (!empty($_POST['dateFrom'])){
										if (empty($_POST['dateTo'])){
											$errors[] = 'Date From and Date To must fill in both';
											$notvalid = true;
										}
									}
									
									if (!empty($_POST['dateTo'])){
										if (empty($_POST['dateFrom'])){
											$errors[] = 'Date From and Date To must fill in both';
											$notvalid = true;
										}
									}
									
									if($notvalid == true){
										echo"
											<div class='section'>
												<ul class='system_messages'>
												<li class='yellow'><span class='ico'></span>
													<strong class='system_title'>Warning!</strong>";
										while (list($key,$value) = each($errors))
										{
												echo "<br/><span class='system_title'>".$value."</span>";
										}
										echo "	</li>
												</ul>
											</div>";
									}else{
										$dateFrom = mysql_real_escape_string($_POST['dateFrom']); 
										$dateTo = mysql_real_escape_string($_POST['dateTo']); 
										$statuscode = mysql_real_escape_string($_POST['statuscode']); 
										$transportid = mysql_real_escape_string($_POST['transportid']); 
										$tdnNo = mysql_real_escape_string($_POST['tdnNo']); 
									}
									
									
								}
								$param = "&dateFrom=$dateFrom&dateTo=$dateTo&statuscode=$statuscode&transportid=$transportid&tdnNo=$tdnNo";
								
							?>
							<form action="" method="POST" class="search_form">
							
							
							<!--[if !IE]>start fieldset<![endif]-->
							<fieldset>
								<!--[if !IE]>start forms<![endif]-->
								<div class="forms">
								
								<!--[if !IE]>start row<![endif]-->
								<div class="row">
									<label>Date From :</label>
									<div class="inputs">
										<span class="input_wrapper" style="width:300px;">
											<input  class="text" id="dateFrom" name="dateFrom" value="<?php echo $dateFrom; ?>" type="text"  />
										</span>
									</div>
								</div>
								<!--[if !IE]>end row<![endif]-->
								
								<!--[if !IE]>start row<![endif]-->
								<div class="row">
									<label>Date To :</label>
									<div class="inputs">
										<span class="input_wrapper" style="width:300px;">
											<input  class="text" id="dateTo" name="dateTo" value="<?php echo $dateTo; ?>" type="text"  />
										</span>
									</div>
								</div>
								<!--[if !IE]>end row<![endif]-->
								
								<?php /* Get data. */
										$qListStat= "select * FROM lookup_status   ";
										$rListStat = $db->sql_fetch($qListStat);
								?>
								<!--[if !IE]>start row<![endif]-->
								<div class="row">
									<label>Status :</label>
									<div class="inputs">
										<span class="input_wrapper" style="width:300px;">
										<select name="statuscode" id="statuscode" >
										<?php 
											echo "<option value='' >Please Select</option>";
											foreach ($rListStat as $i) {	
												echo "<option value='$i[code]' "; 
													if($statuscode==$i['code']) 
														echo 'selected'; 
												echo ">$i[name]</option>";
											} ?>
										</select>
										
									</span>
									</div>
								</div>
								<!--[if !IE]>end row<![endif]-->
								
								<?php /* Get data. */
										$qListTrn= "select * FROM transport  ";
										$rListTrn = $db->sql_fetch($qListTrn);
								?>
								<!--[if !IE]>start row<![endif]-->
								<div class="row">
									<label>Transport :</label>
									<div class="inputs">
										<span class="input_wrapper" style="width:300px;">
										<select name="transportid" id="transportid" >
										<?php 
											echo "<option value='' >Please Select</option>";
											foreach ($rListTrn as $i) {	
												echo "<option value='$i[trn_id]' "; 
													if($transportid==$i['trn_id']) 
														echo 'selected'; 
												echo ">$i[trn_plate_no]</option>";
											} ?>
										</select>
										
									</span>
									</div>
								</div>
								<!--[if !IE]>end row<![endif]-->
								
								<!--[if !IE]>start row<![endif]-->
								<div class="row">
									<label>TDN No. :</label>
									<div class="inputs">
										<span class="input_wrapper" style="width:300px;">
											<input  class="text" id="tdnNo" name="tdnNo" value="<?php echo $tdnNo; ?>" type="text"  />
										</span>
									</div>
								</div>
								<!--[if !IE]>end row<![endif]-->
								
								<!--[if !IE]>start row<![endif]-->
								<div class="row">
									<div class="inputs">
										<!--span class="button blue_button"><span><span><em>Reset</em></span></span><input id="reset" name="reset" type="submit" /></span-->
									
										<span class="button blue_button search_button"><span><span><em>Search</em></span></span><input id="search" name="search" type="submit" /></span>
									</div>
								</div>
								<!--[if !IE]>end row<![endif]-->
								
								
								
								</div>
								<!--[if !IE]>end forms<![endif]-->
								
							</fieldset>
							<!--[if !IE]>end fieldset<![endif]-->
							
							
							</form>

							<?php
								/* 
									   First get total number of rows in data table. 
									   If you have a WHERE clause in your query, make sure you mirror it here.
									*/
									
									$dateClause = "";
									if($dateFrom != '' && $dateTo != ''){
										$dateFrom 	= changeDateBeginYear($dateFrom );
										$dateTo 	= changeDateBeginYear($dateTo );
										$dateClause = " AND tpd.tpd_delivered_date BETWEEN  '$dateFrom' AND  '$dateTo'   ";
									}
									
									$statuscodeClause = "";
									if($statuscode != ''){
										$statuscodeClause = " AND tpd.tpd_status = '$statuscode' ";
									}
									
									$transportidClause = "";
									$transportidFrom = "";
									if($transportid != ''){
										$transportidFrom = " ,transport t ";
										$transportidClause = " AND t.trn_id = tpd.tpd_trn_id AND t.trn_id = '$transportid' ";
									}
									
									$tdnNoClause = "";
									if($tdnNo != ''){
										$tdnNoClause = " AND tpd.tpd_tdn_no = '$tdnNo' ";
									}
									
									
									$qCount= "SELECT COUNT(*)  FROM transport_package_delivering tpd,
										  entity s,
										  entity r 
										  $transportidFrom
										WHERE tpd.tpd_sender_ent_id = s.ent_id
											AND tpd.tpd_receiver_ent_id = r.ent_id 
											$transportidClause
											$dateClause
											$statuscodeClause 
											$tdnNoClause  ";
									$total_pages = $db->sql_total($qCount);
									/* Setup vars for query. */
									$limit = 10; 								//how many items to show per page
							
							?>
							<div class="table_tabs_menu">
								<h3>Total <?php  echo $total_pages;  ?> records</h3>
								<br />
							</div>
							<!--[if !IE]>start table_wrapper<![endif]-->
							<div class="table_wrapper">
								<div class="table_wrapper_inner">
								
								<?php  
									$page="";
									if(isset($_GET['page']))
										$page=$_GET['page'];
									
									if($page) 
										$start = ($page - 1) * $limit; 			//first item to display on this page
									else
										$start = 0;								//if no page var is given, set start to 0
									/* Get data. */
									$qList= "SELECT
										  tpd.*,
										  s.ent_id           AS sid,
										  s.ent_short_name   AS sname,
										  r.ent_id           AS rid,
										  r.ent_short_name   AS rname,
										  r.ent_add_postcode AS rpostcode
										FROM transport_package_delivering tpd,
										  entity s,
										  entity r 
										  $transportidFrom
										WHERE tpd.tpd_sender_ent_id = s.ent_id
											AND tpd.tpd_receiver_ent_id = r.ent_id 
											$transportidClause
											$dateClause
											$statuscodeClause 
											$tdnNoClause
											ORDER BY tpd_modified_date desc, r.ent_add_postcode DESC
												LIMIT $start, $limit";
									$rList = $db->sql_fetch($qList);			

								?>
								<table cellpadding="0" cellspacing="0" width="100%">
									<tbody>
									<tr>
										<th style="width: 10px;">Bil.</th>
										<th style="width: 50px;">TDN No.</th>
										<th style="width: 50px;">DO No.</th>
										<th style="width: 50px;">Quatity</th>
										<th>Sender</th>
										<th>Receiver</th>
										<th>Destination</th>
										<th>Status</th>
										<th>Action</th>
										
									</tr>
								<?php
									$num =$start;
									foreach ($rList as $i) {
										$num++;
										$colorRow = "first";
										if($num%2==0)
											$colorRow = "second"; 
										else
											$colorRow = "first";
										
										$qCity= "SELECT * from lookup_postcode where postcode = '$i[rpostcode]' ";
										$rCity = $db->sql_list($qCity);
				
										echo '	
											<tr class="'.$colorRow.'">
												<td>'.$num.'</td>
												<td>'.$i['tpd_tdn_no'].'</td>
												<td>'.$i['tpd_do_ref'].'</td>
												<td>'.$i['tpd_qty'].' '.$i['tpd_qty_type'].'</td>
												<td>'.$i['sname'].'</td>
												<td>'.$i['rname'].'</td>
												<td>'.$i['rpostcode'].' '.$rCity['post_office'].'</td>
												<td>'.getStatus($i['tpd_status']).'</td>
												<td>
													<div class="actions_menu" >
														<ul>
															<li><a class="details" href="packageview.php?id='.$i['tpd_id'].'">View</a></li>
														</ul>
													</div>
												</td>
											</tr>';
										
									}
								?>
								</tbody></table>
								</div>
								</div>
								
							</div><!--[if !IE]>end table_wrapper<![endif]-->
							
							<!--[if !IE]>start pagination<![endif]-->
							<div class="pagination_wrapper">
							<span class="pagination_top"></span>
							<div class="pagination_middle">
							<div class="pagination">
								<?php
								// How many adjacent pages should be shown on each side?
									$adjacents = 3;
								/* Setup page vars for display. */
									if ($page == 0) $page = 1;					//if no page var is given, default to 1.
									$prev = $page - 1;							//previous page is page - 1
									$next = $page + 1;							//next page is page + 1
									$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
									$lpm1 = $lastpage - 1;						//last page minus 1
								/* 
									Now we apply our rules and draw the pagination object. 
									We're actually saving the code to a variable in case we want to draw it more than once.
								*/
								echo '<span class="page_no">'.$lang['CONTENT_PAGINATION_PAGE'].' '.$page.' of '.$lastpage.' </span>';
								echo '<ul class="pag_list">';

									//previous button
									if ($page > 1) 
										echo '<li><a href="reportpackage.php?page='.$prev.$param.'" class="pag_nav"><span><span>'.$lang['CONTENT_PAGINATION_PREVIOUS'].'</span></span></a> </li>';
									else
										echo '<li><a href="#" class="pag_nav"><span><span>'.$lang['CONTENT_PAGINATION_PREVIOUS'].'</span></span></a> </li>';

									//pages	
									if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
									{	
										for ($counter = 1; $counter <= $lastpage; $counter++)
										{
											if ($counter == $page)
												echo '<li><a href="#" class="current_page"><span><span>'.$counter.'</span></span></a></li>';
											else	
												echo '<li><a href="reportpackage.php?page='.$counter.$param.'">'.$counter.'</a></li>';	
										}
									}
									elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
									{
										//close to beginning; only hide later pages
										if($page < 1 + ($adjacents * 2))		
										{
											for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
											{
												if ($counter == $page)
													echo '<li><a href="#" class="current_page"><span><span>'.$counter.'</span></span></a></li>';
												else	
													echo '<li><a href="reportpackage.php?page='.$counter.$param.'">'.$counter.'</a></li>';													
											}
											echo '<li>[...]</li>';
											echo '<li><a href="reportpackage.php?page='.$lpm1.$param.'">'.$lpm1.'</a></li>';	
											echo '<li><a href="reportpackage.php?page='.$lastpage.$param.'">'.$lastpage.'</a></li>';		
										}
										//in middle; hide some front and some back
										elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
										{
											echo '<li><a href="reportpackage.php?page=1'.$param.'">1</a></li>';	
											echo '<li><a href="reportpackage.php?page=2'.$param.'">2</a></li>';
											echo '<li>[...]</li>';											
											for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
											{
												if ($counter == $page)
													echo '<li><a href="#" class="current_page"><span><span>'.$counter.'</span></span></a></li>';
												else
													echo '<li><a href="reportpackage.php?page='.$counter.$param.'">'.$counter.'</a></li>';						
											}
											echo '<li>[...]</li>';
											echo '<li><a href="reportpackage.php?page='.$lpm1.$param.'">'.$lpm1.'</a></li>';	
											echo '<li><a href="reportpackage.php?page='.$lastpage.$param.'">'.$lastpage.'</a></li>';												
										}
										//close to end; only hide early pages
										else
										{
											echo '<li><a href="reportpackage.php?page=1'.$param.'">1</a></li>';	
											echo '<li><a href="reportpackage.php?page=2'.$param.'">2</a></li>';
											echo '<li>[...]</li>';	
											for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
											{
												if ($counter == $page)
													echo '<li><a href="#" class="current_page"><span><span>'.$counter.'</span></span></a></li>';
												else
													echo '<li><a href="reportpackage.php?page='.$counter.$param.'">'.$counter.'</a></li>';						
											}
										}
									}
									//next button
									if ($page < $counter - 1) 
										echo '<li><a href="reportpackage.php?page='.$next.$param.'" class="pag_nav"><span><span>'.$lang['CONTENT_PAGINATION_NEXT'].'</span></span></a> </li>';
									else
										echo '<li><a href="#" class="pag_nav"><span><span>'.$lang['CONTENT_PAGINATION_NEXT'].'</span></span></a> </li>';
									?>
								</ul>
							</div>
							</div>
							<span class="pagination_bottom"></span>
							</div>
						<!--[if !IE]>end pagination<![endif]-->
							
							
						
						<span class="section_content_bottom"></span>
					</div>
					<!--[if !IE]>end section content<![endif]-->
				</div>
				<!--[if !IE]>end section<![endif]-->
				
					
				
			</div>
		</div>
		<!--[if !IE]>end content<![endif]-->

<?php
	include('footer.php');
?>